home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / TEX-UTIL / DVIPS_55 / dvips / src / c / resident < prev    next >
Text File  |  1994-05-06  |  26KB  |  972 lines

  1. /*   For use with emTeX set FONTPATH to "TEXTFM"
  2.  */
  3. #ifndef FONTPATH
  4. #define FONTPATH "TEXFONTS"
  5. #endif
  6.  
  7. /*
  8.  *   This code reads in and handles the defaults for the program from the
  9.  *   file config.sw.  This entire file is a bit kludgy, sorry.
  10.  */
  11. #include "dvips.h" /* The copyright notice in that file is included too! */
  12. #include "paths.h"
  13. /*
  14.  *   This is the structure definition for resident fonts.  We use
  15.  *   a small and simple hash table to handle these.  We don't need
  16.  *   a big hash table.
  17.  */
  18. struct resfont *reshash[RESHASHPRIME] ;
  19. /*
  20.  *   These are the external routines we use.
  21.  */
  22. extern void error() ;
  23. extern integer scalewidth() ;
  24. extern int tfmload() ;
  25. extern FILE *search() ;
  26. extern shalfword pkbyte() ;
  27. extern integer pkquad() ;
  28. extern integer pktrio() ;
  29. extern Boolean pkopen() ;
  30. extern char *getenv() ;
  31. extern char *newstring() ;
  32. extern int add_header() ;
  33. extern int add_name() ;
  34. extern char *get_name() ;
  35. extern int system() ;
  36. extern void handlepapersize() ;
  37. extern void checkstrings() ;
  38. void getpsinfo() ;
  39. extern void *revlist() ;
  40. /*
  41.  *   These are the external variables we use.
  42.  */
  43. #ifdef DEBUG
  44. extern integer debug_flag;
  45. #endif  /* DEBUG */
  46. extern integer pagecopies ;
  47. extern int overridemag ;
  48. extern long bytesleft ;
  49. extern quarterword *raster ;
  50. extern FILE *pkfile ;
  51. extern char *oname ;
  52. extern integer swmem, fontmem ;
  53. /* extern char *tfmpath, *pictpath ;
  54. extern char *pkpath ;
  55. extern char *vfpath ;
  56. extern char *figpath ;
  57. extern char *configpath ; */
  58. extern Boolean noenv ;
  59. extern Boolean downloadpspk ;
  60. #ifdef SEARCH_SUBDIRECTORIES
  61. extern char *fontsubdirpath ;
  62. #endif
  63. #ifdef FONTLIB
  64. extern char *flipath, *fliname ;
  65. #endif
  66. /* extern char *headerpath ; */
  67. extern char *paperfmt ; 
  68. extern char *nextstring ;
  69. extern char *maxstring ;
  70. extern char *warningmsg ;
  71. extern Boolean disablecomments ;
  72. extern Boolean compressed ;
  73. extern int quiet ;
  74. extern int filter ;
  75. extern Boolean reverse ;
  76. extern Boolean usesPSfonts ;
  77. extern Boolean nosmallchars ;
  78. extern Boolean removecomments ;
  79. extern Boolean safetyenclose ;
  80. extern Boolean dopprescan ;
  81. extern integer maxsecsize ;
  82. extern integer mag ;
  83. extern Boolean sepfiles ;
  84. extern int actualdpi ;
  85. extern int vactualdpi ;
  86. extern int maxdrift ;
  87. extern int vmaxdrift ;
  88. extern char *printer ;
  89. extern char *mfmode ;
  90. extern Boolean sendcontrolD ;
  91. extern int lastresortsizes[] ;
  92. extern integer hoff, voff ;
  93. extern struct papsiz *papsizes ;
  94. extern Boolean secure ;
  95. extern integer hpapersize, vpapersize ;
  96. extern int landscape ;
  97. /*
  98.  *   To maintain a list of document fonts, we use the following
  99.  *   pointer.
  100.  */
  101. struct header_list *ps_fonts_used ;
  102. /*
  103.  *   Our hash routine.
  104.  */
  105. int
  106. hash(s)
  107.    char *s ;
  108. {
  109.    int h = 12 ;
  110.  
  111.    while (*s != 0)
  112.       h = (h + h + *s++) % RESHASHPRIME ;
  113.    return(h) ;
  114. }
  115. /*
  116.  *   Reverse the hash chains.
  117.  */
  118. void
  119. revpslists() {
  120.    register int i ;
  121.    for (i=0; i<RESHASHPRIME; i++)
  122.       reshash[i] = (struct resfont *)revlist(reshash[i]) ;
  123. }
  124. /*
  125.  *   cleanres() marks all resident fonts as not being yet sent.
  126.  */
  127. void
  128. cleanres() {
  129.    register int i ;
  130.    register struct resfont *p ;
  131.    for (i=0; i<RESHASHPRIME; i++)
  132.       for (p=reshash[i]; p; p=p->next)
  133.          p->sent = 0 ;
  134. }
  135. /*
  136.  *   The routine that looks up a font name.
  137.  */
  138. struct resfont *
  139. lookup(name)
  140.    char *name ;
  141. {
  142.    struct resfont *p ;
  143.  
  144.    for (p=reshash[hash(name)]; p!=NULL; p=p->next)
  145.       if (strcmp(p->Keyname, name)==0)
  146.          return(p) ;
  147.    return(NULL) ;
  148. }
  149. /*
  150.  *   This routine adds an entry.
  151.  */
  152. void
  153. add_entry(TeXname, PSname, specinfo, downloadinfo)
  154.    char *TeXname, *PSname, *specinfo, *downloadinfo ;
  155. {
  156.    struct resfont *p ;
  157.    int h ;
  158.  
  159.    if (PSname == NULL)
  160.       PSname = TeXname ;
  161.    p = (struct resfont *)mymalloc((integer)sizeof(struct resfont)) ;
  162.    p->Keyname = TeXname ;
  163.    p->PSname = PSname ;
  164.    p->TeXname = TeXname ;
  165.    p->specialinstructions = specinfo ;
  166.    if (downloadinfo && *downloadinfo)
  167.       p->downloadheader = downloadinfo ;
  168.    else
  169.       p->downloadheader = 0 ;
  170.    h = hash(TeXname) ;
  171.    p->next = reshash[h] ;
  172.    p->sent = 0 ;
  173.    reshash[h] = p ;
  174. }
  175. /*
  176.  *   Now our residentfont routine.  Returns the number of characters in
  177.  *   this font, based on the TFM file.
  178.  */
  179. extern char *infont ;
  180. int
  181. residentfont(curfnt)
  182.         register fontdesctype *curfnt ;
  183. {
  184.    register shalfword i ;
  185.    struct resfont *p ;
  186.  
  187. /*
  188.  *   First we determine if we can find this font in the resident list.
  189.  */
  190.    if (*curfnt->area)
  191.       return 0 ; /* resident fonts never have a nonstandard font area */
  192.    if ((p=lookup(curfnt->name))==NULL)
  193.       return 0 ;
  194. /*
  195.  *   This is not yet the correct way to do things, but it is useful as it
  196.  *   is so we leave it in.  The problem:  if resident Times-Roman is
  197.  *   re-encoded, then it will be downloaded as bitmaps; this is not
  198.  *   right.  The solution will be to introduce two types of `<'
  199.  *   directives, one that downloads fonts and one that downloads
  200.  *   short headers that are innocuous.
  201.  */
  202.    if (p->downloadheader && downloadpspk) {
  203. #ifdef DEBUG
  204.       if (dd(D_FONTS))
  205.          (void)fprintf(stderr,"Using PK font %s for <%s>.\n",
  206.                                      curfnt->name, p->PSname) ;
  207. #endif  /* DEBUG */
  208.       return 0 ;
  209.    }
  210. /*
  211.  *   We clear out some pointers:
  212.  */
  213. #ifdef DEBUG
  214.    if (dd(D_FONTS))
  215.         (void)fprintf(stderr,"Font %s <%s> is resident.\n",
  216.                                      curfnt->name, p->PSname) ;
  217. #endif  /* DEBUG */
  218.    curfnt->resfont = p ;
  219.    curfnt->name = p->TeXname ;
  220.    for (i=0; i<256; i++) {
  221.       curfnt->chardesc[i].TFMwidth = 0 ;
  222.       curfnt->chardesc[i].packptr = NULL ;
  223.       curfnt->chardesc[i].pixelwidth = 0 ;
  224.       curfnt->chardesc[i].flags = 0 ;
  225.    }
  226.    add_name(p->PSname, &ps_fonts_used) ;
  227. /*
  228.  *   We include the font here.  But we only should need to include the
  229.  *   font if we have a stupid spooler; smart spoolers should be able
  230.  *   to supply it automatically.
  231.  */
  232.    if (p->downloadheader) {
  233.       char *cp = p->downloadheader ;
  234.       char *q ;
  235.  
  236.       infont = p->PSname ;
  237.       while (1) {
  238.          q = cp ;
  239.          while (*cp && *cp != ' ')
  240.             cp++ ;
  241.          if (*cp) {
  242.             *cp = 0 ;
  243.             add_header(q) ;
  244.             *cp++ = ' ' ;
  245.          } else {
  246.             add_header(q) ;
  247.             break ;
  248.          }
  249.          infont = 0 ;
  250.       }
  251.       infont = 0 ;
  252.    }
  253.    i = tfmload(curfnt) ;
  254.    if (i < 0)
  255.       i = 1 ;
  256.    usesPSfonts = 1 ;
  257.    return(i) ;
  258. }
  259. #define INLINE_SIZE (500)
  260. static char was_inline[INLINE_SIZE] ;
  261. void
  262. bad_config() {
  263.    extern void exit() ;
  264.  
  265.    error("Error in config file:") ;
  266.    (void)fprintf(stderr, "%s\n", was_inline) ;
  267.    exit(1) ;
  268. }
  269. /*
  270.  *   Get environment variables! These override entries in ./config.h.
  271.  *   We substitute everything of the form ::, ^: or :$ with default,
  272.  *   so a user can easily build on to the existing paths.
  273.  */
  274. static char *getpath(who, what)
  275. char *who, *what ;
  276. {
  277.    if (who) {
  278.       register char *pp, *qq ;
  279.       int lastsep = 1 ;
  280.  
  281.       for (pp=nextstring, qq=who; *qq;) {
  282.          if (*qq == PATHSEP) {
  283.             if (lastsep) {
  284.                strcpy(pp, what) ;
  285.                pp = pp + strlen(pp) ;
  286.             }
  287.             lastsep = 1 ;
  288.          } else
  289.             lastsep = 0 ;
  290.          *pp++ = *qq++ ;
  291.       }
  292.       if (lastsep) {
  293.          strcpy(pp, what) ;
  294.          pp = pp + strlen(pp) ;
  295.       }
  296.       *pp = 0 ;
  297.       qq = nextstring ;
  298.       nextstring = pp + 1 ;
  299.       return qq ;
  300.    } else
  301.       return what ;
  302. }
  303. /*
  304.  *   We use this function so we can support strings delimited by
  305.  *   double quotes with spaces in them.  We also accept strings
  306.  *   with spaces in them, but kill off any spaces at the end.
  307.  */
  308. char *configstring(s, nullok)
  309. char *s ;
  310. int nullok ;
  311. {
  312.    char tstr[300] ;
  313.    char *p = tstr ;
  314.  
  315.    while (*s && *s <= ' ')
  316.       s++ ;
  317.    if (*s == '"') {
  318.       s++ ;
  319.       while (*s != 10 && *s != 0 && *s != '"' && p < tstr+290)
  320.          *p++ = *s++ ;
  321.    } else {
  322.       while (*s && p < tstr+290)
  323.          *p++ = *s++ ;
  324.       while (*(p-1) <= ' ' && p > tstr)
  325.          p-- ;
  326.    }
  327.    *p = 0 ;
  328.    if (p == tstr && ! nullok)
  329.       bad_config() ;
  330.    return newstring(tstr) ;
  331. }
  332. /*
  333.  *   Now we have the getdefaults routine.
  334.  */
  335. static char *psmapfile = PSMAPFILE ;
  336. void
  337. getdefaults(s)
  338. char *s ;
  339. {
  340.    FILE *deffile ;
  341.    char PSname[300] ;
  342.    register char *p ;
  343.    int i, j ;
  344.    integer hsiz, vsiz ;
  345.    char *d = configpath ;
  346.    int canaddtopaper = 0 ;
  347.  
  348.    if (printer == NULL) {
  349.       if (s) {
  350.          strcpy(PSname, s) ;
  351.       } else {
  352. #ifndef VMCMS  /* IBM: VM/CMS - don't have home directory on VMCMS */
  353. #ifndef MVSXA
  354.          d = "~" ;
  355. #endif
  356. #endif  /* IBM: VM/CMS */
  357.          strcpy(PSname, DVIPSRC) ;
  358.       }
  359.    } else {
  360. #if defined(MSDOS) || defined(OS2)
  361.       strcpy(PSname, printer) ;
  362.       strcat(PSname, ".cfg") ;
  363. #else
  364.       strcpy(PSname, "config.") ;
  365.       strcat(PSname, printer) ;
  366. #endif
  367.    }
  368.    if ((deffile=search(d,PSname,READ))!=NULL) {
  369.       while (fgets(was_inline, INLINE_SIZE, deffile)!=NULL) {
  370. /*
  371.  *   We need to get rid of the newline.
  372.  */
  373.        for (p=was_inline; *p; p++) ;
  374.        if (p > was_inline) *(p-1) = 0 ;
  375.        if (was_inline[0] != '@')
  376.           canaddtopaper = 0 ;
  377.        switch (was_inline[0]) {
  378. /*
  379.  *   Handling paper size information:
  380.  *
  381.  *      If line is empty, then we clear out the paper size information
  382.  *      we have so far.
  383.  *
  384.  *      If it is `@+', then we add to the current paper size info.
  385.  *
  386.  *      If it is `name hsize vsize', then we start a new definition.
  387.  */
  388. case '@' :
  389.          p = was_inline + 1 ;
  390.          while (*p && *p <= ' ') p++ ;
  391.          if (*p == 0) {
  392.             papsizes = 0 ; /* throw away memory */
  393.          } else if (*p == '+') {
  394.             if (canaddtopaper == 0)
  395.                error(
  396.       "can't have @+ in config file not immediately following a non-@ line") ;
  397.             else {
  398.                *(nextstring-1) = '\n' ;/* IBM: VM/CMS - changed 10 to "\n" */
  399.                p++ ;
  400.                while (*p && *p == ' ') p++ ;
  401.                strcpy(nextstring, p) ;
  402.                nextstring += strlen(p) + 1 ;
  403.             }
  404.          } else {
  405.             struct papsiz *ps ;
  406.             
  407.             ps = (struct papsiz *)mymalloc((integer)sizeof(struct papsiz)) ;
  408.             ps->next = papsizes ;
  409.             papsizes = ps ;
  410.             ps->name = p ;
  411.             while (*p && *p > ' ')
  412.                p++ ;
  413.             *p++ = 0 ;
  414.             ps->name = newstring(ps->name) ;
  415.             while (*p && *p <= ' ') p++ ;
  416.             handlepapersize(p, &hsiz, &vsiz) ;
  417.             ps->xsize = hsiz ;
  418.             ps->ysize = vsiz ;
  419.             ps->specdat = nextstring++ ;
  420.             canaddtopaper = 1 ;
  421.          }
  422.          break ;
  423. case 'a' :
  424.          dopprescan = (was_inline[1] != '0') ;
  425.          break ;
  426. case 'b':
  427. #ifdef SHORTINT
  428.          if (sscanf(was_inline+1, "%ld", &pagecopies) != 1) bad_config() ;
  429. #else
  430.          if (sscanf(was_inline+1, "%d", &pagecopies) != 1) bad_config() ;
  431. #endif
  432.          if (pagecopies < 1 || pagecopies > 1000)
  433.             bad_config() ;
  434.          break ;
  435. case 'm' :
  436. #ifdef SHORTINT
  437.          if (sscanf(was_inline+1, "%ld", &swmem) != 1) bad_config() ;
  438. #else   /* ~SHORTINT */
  439.          if (sscanf(was_inline+1, "%d", &swmem) != 1) bad_config() ;
  440. #endif  /* ~SHORTINT */
  441.          swmem += fontmem ; /* grab headers we've seen already */
  442.          break ;
  443. case 'M' :
  444.          mfmode = configstring(was_inline+1, 0) ;
  445.          break ;
  446. case 'o' :
  447.          oname = configstring(was_inline+1, 1) ;
  448.          if (*oname && oname[strlen(oname)-1] == ':')
  449.             sendcontrolD = 1 ; /* if we send to a device, *we* are spooler */
  450.          break ;
  451. case 'O' :
  452.          p = was_inline + 1 ;
  453.          handlepapersize(p, &hoff, &voff) ;
  454.          break ;
  455. #ifdef FONTLIB
  456. case 'L' : 
  457.          {
  458.             char tempname[300] ;
  459.             extern char *fliparse() ;
  460.             if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  461.             else {
  462.                flipath = getpath(fliparse(PSname,tempname), flipath);
  463.                fliname = newstring(tempname) ;
  464.             }
  465.      }
  466.          break ;
  467. #endif
  468. /* case 'T' : 
  469.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  470.          else tfmpath = getpath(PSname, tfmpath) ;
  471.          break ;
  472. case 'P' :
  473.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  474.          else pkpath = getpath(PSname, pkpath) ;
  475.          break ;
  476. case 'p' :
  477.          p = was_inline + 1 ;
  478.          while (*p && *p <= ' ')
  479.             p++ ;
  480.          if (*p == '+') {
  481.             if (sscanf(p+1, "%s", PSname) != 1) bad_config() ;
  482.             getpsinfo(PSname) ;
  483.          } else {
  484.             psmapfile = configstring(was_inline+1, 0) ;
  485.          }
  486.          break ;
  487. case 'v' : case 'V' :
  488.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  489.          else vfpath = getpath(PSname, vfpath) ;
  490.          break ;
  491. case 'S' :
  492.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  493.          else figpath = getpath(PSname, figpath) ;
  494.          break ; */
  495. case 's':
  496.          safetyenclose = 1 ;
  497.          break ;
  498. /* case 'H' : 
  499.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  500.          else headerpath = getpath(PSname, headerpath) ;
  501.          break ; */
  502. case '%': case ' ' : case '*' : case '#' : case ';' :
  503. case '=' : case 0 : case '\n' :
  504.          break ;
  505. case 'r' :
  506.          reverse = (was_inline[1] != '0') ;
  507.          break ;
  508. /*
  509.  *   This case is for last resort font scaling; I hate this, but enough
  510.  *   people have in no uncertain terms demanded it that I'll go ahead and
  511.  *   add it.
  512.  *
  513.  *   This line must have numbers on it, resolutions, to search for the
  514.  *   font as a last resort, and then the font will be scaled.  These
  515.  *   resolutions should be in increasing order.
  516.  *
  517.  *   For most machines, just `300' is sufficient here; on the NeXT,
  518.  *   `300 400' may be more appropriate.
  519.  */
  520. case 'R':
  521.          i = 0 ;
  522.          p = was_inline + 1 ;
  523.          while (*p) {
  524.             while (*p && *p <= ' ')
  525.                p++ ;
  526.             if ('0' <= *p && *p <= '9') {
  527.                j = 0 ;
  528.                while ('0' <= *p && *p <= '9')
  529.                   j = 10 * j + (*p++ - '0') ;
  530.                if (i > 0)
  531.                   if (lastresortsizes[i-1] > j) {
  532.                      error("last resort sizes (R) must be sorted") ;
  533.                      bad_config() ;
  534.                   }
  535.                lastresortsizes[i++] = j ;
  536.             } else {
  537.                if (*p == 0)
  538.                   break ;
  539.                error("! only numbers expected on `R' line in config!") ;
  540.             }
  541.          }
  542.          lastresortsizes[i] = 32000 ;
  543.          break ;
  544. case 'D' :
  545.          if (sscanf(was_inline+1, "%d", &actualdpi) != 1) bad_config() ;
  546.          if (actualdpi < 10 || actualdpi > 10000) bad_config() ;
  547.      vactualdpi = actualdpi;
  548.          break ;
  549. /*
  550.  *   Execute a command.  This can be dangerous, but can also be very useful.
  551.  */
  552. case 'E' :
  553. #ifdef SECURE
  554.          error("dvips was compiled with SECURE, which disables E in config") ;
  555. #else
  556.          if (secure) {
  557.             error("dvips -R option used, which disables E in config") ;
  558.             break ;
  559.          }
  560.          (void)system(was_inline+1) ;
  561. #endif
  562.          break ;
  563. case 'K':
  564.          removecomments = (was_inline[1] != '0') ;
  565.          break ;
  566. case 'U':
  567.          nosmallchars = (was_inline[1] != '0') ;
  568.          break ;
  569. case 'W':
  570.          for (p=was_inline+1; *p && *p <= ' '; p++) ;
  571.          if (*p)
  572.             warningmsg = newstring(p) ;
  573.          else
  574.             warningmsg = 0 ;
  575.          break ;
  576. case 'X' :
  577.          if (sscanf(was_inline+1, "%d", &actualdpi) != 1) bad_config() ;
  578.          if (actualdpi < 10 || actualdpi > 10000) bad_config() ;
  579.          break ;
  580. case 'Y' :
  581.          if (sscanf(was_inline+1, "%d", &vactualdpi) != 1) bad_config() ;
  582.          if (vactualdpi < 10 || vactualdpi > 10000) bad_config() ;
  583.          break ;
  584. case 'x': case 'y':
  585.          if (sscanf(was_inline+1, "%d", &mag) != 1) bad_config() ;
  586.          overridemag = (was_inline[0] == 'x') ? 1 : -1 ;
  587.          break ;
  588. case 'e' :
  589.          if (sscanf(was_inline+1, "%d", &maxdrift) != 1) bad_config() ;
  590.          if (maxdrift < 0) bad_config() ;
  591.      vmaxdrift = maxdrift;
  592.          break ;
  593. case 'q' : case 'Q' :
  594.          quiet = (was_inline[1] != '0') ;
  595.          break ;
  596. case 'f' : case 'F' :
  597.          filter = (was_inline[1] != '0') ;
  598.          break ;
  599. case 'h' : 
  600.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  601.          else (void)add_header(PSname) ;
  602.          break ;
  603. case 'i' :
  604.          if (sscanf(was_inline+1, "%d", &maxsecsize) != 1)
  605.             maxsecsize = 0 ;
  606.          sepfiles = 1 ;
  607.          break ;
  608. case 'I':
  609.          noenv = (was_inline[1] != '0') ;
  610.          break ;
  611. case 'N' :
  612.          disablecomments = (was_inline[1] != '0') ;
  613.          break ;
  614. case 'Z' :
  615.          compressed = (was_inline[1] != '0') ;
  616.          break ;
  617. case 't' :
  618.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  619.          else {
  620.            if (strcmp(PSname, "landscape") == 0) {
  621.                if (hpapersize || vpapersize)
  622.                   error(
  623.             "both landscape and papersize specified; ignoring landscape") ;
  624.                else
  625.                   landscape = 1 ;
  626.             } else
  627.                paperfmt = newstring(PSname) ;
  628.          }
  629.          break ;
  630. default:
  631.          bad_config() ;
  632.       }
  633.      }
  634.      (void)fclose(deffile) ;
  635.    } else {
  636.       if (printer)
  637.          error("! no such printer (can't find corresponding config file)") ;
  638.    }
  639. }
  640.  
  641. /*
  642.  *   If a character pointer is passed in, use that name; else, use the
  643.  *   default (possibly set) name.
  644.  */
  645. void getpsinfo(name)
  646. char *name ;
  647. {
  648.    FILE *deffile ;
  649.    register char *p ;
  650.    char *specinfo, *downloadinfo ;
  651.    char downbuf[200] ;
  652.  
  653.    if (name == 0)
  654.       name = psmapfile ;
  655.    if ((deffile=search(configpath, name, READ))!=NULL) {
  656.       while (fgets(was_inline, INLINE_SIZE, deffile)!=NULL) {
  657.          p = was_inline ;
  658.          if (*p > ' ' && *p != '*' && *p != '#' && *p != ';' && *p != '%') {
  659.             char *TeXname = NULL ;
  660.             char *PSname = NULL ;
  661.             specinfo = NULL ;
  662.             downloadinfo = NULL ;
  663.             downbuf[0] = 0 ;
  664.             while (*p) {
  665.                while (*p && *p <= ' ')
  666.                   p++ ;
  667.                if (*p) {
  668.                   if (*p == '"')
  669.                      specinfo = p + 1 ;
  670.                   else if (*p == '<') {
  671.                      if (downloadinfo) {
  672.                         strcat(downbuf, downloadinfo) ;
  673.                         strcat(downbuf, " ") ;
  674.                      }
  675.                      downloadinfo = p + 1 ;
  676.                   } else if (TeXname)
  677.                      PSname = p ;
  678.                   else
  679.                      TeXname = p ;
  680.                   if (*p == '"') {
  681.                      p++ ;
  682.                      while (*p != '"' && *p)
  683.                         p++ ;
  684.                   } else
  685.                      while (*p > ' ')
  686.                         p++ ;
  687.                   if (*p)
  688.                      *p++ = 0 ;
  689.                }
  690.             }
  691.             if (downloadinfo)
  692.                strcat(downbuf, downloadinfo) ;
  693.             if (TeXname) {
  694.                TeXname = newstring(TeXname) ;
  695.                specinfo = newstring(specinfo) ;
  696.                PSname = newstring(PSname) ;
  697.                downloadinfo = newstring(downbuf) ;
  698.                add_entry(TeXname, PSname, specinfo, downloadinfo) ;
  699.             }
  700.         }
  701.       }
  702.       (void)fclose(deffile) ;
  703.    }
  704.    checkstrings() ;
  705. }
  706. /*
  707.  *   Get environment variables! These override entries in ./config.h.
  708.  *   We substitute everything of the form ::, ^: or :$ with default,
  709.  *   so a user can easily build on to the existing paths.
  710.  */
  711. static char *getenvup(who, what)
  712. char *who, *what ;
  713. {
  714.    return getpath(getenv(who), what) ;
  715. }
  716. #ifdef SEARCH_SUBDIRECTORIES
  717. static char *concat3();
  718. #endif
  719. void checkenv(which)
  720. int which ;
  721. {
  722. /*   if (which) {
  723.       tfmpath = getenvup(FONTPATH, tfmpath) ;
  724.       vfpath = getenvup("VFFONTS", vfpath) ;
  725.       pictpath = getenvup("TEXPICTS", pictpath) ;
  726.       figpath = getenvup("TEXINPUTS", figpath) ;
  727.       headerpath = getenvup("DVIPSHEADERS", headerpath) ;
  728.       if (getenv("TEXPKS"))
  729.          pkpath = getenvup("TEXPKS", pkpath) ;
  730.       else if (getenv("TEXPACKED"))
  731.          pkpath = getenvup("TEXPACKED", pkpath) ;
  732.       else if (getenv("PKFONTS"))
  733.          pkpath = getenvup("PKFONTS", pkpath) ;   
  734. #ifdef SEARCH_SUBDIRECTORIES
  735.       else if (getenv(FONTPATH))
  736.          pkpath = getenvup(FONTPATH, pkpath) ;
  737.       if (getenv ("TEXFONTS_SUBDIR"))
  738.          fontsubdirpath = getenvup ("TEXFONTS_SUBDIR", fontsubdirpath);
  739.       {
  740.          char pathsep[2] ;
  741.          char *do_subdir_path();
  742.          char *dirs = do_subdir_path (fontsubdirpath);
  743.         
  744.          pathsep[0] = PATHSEP ;
  745.          pathsep[1] = '\0' ;
  746.          tfmpath = concat3 (tfmpath, pathsep, dirs);
  747.          pkpath = concat3 (pkpath, pathsep, dirs);
  748.       }
  749. #endif
  750.    } else
  751.       configpath = getenvup("TEXCONFIG", configpath) ;*/
  752. }
  753.  
  754. #ifdef SEARCH_SUBDIRECTORIES
  755.  
  756. #include <sys/types.h>
  757. #include <sys/stat.h>
  758. #include <errno.h>
  759.  
  760. #ifdef SYSV
  761. #include <dirent.h>
  762. typedef struct dirent *directory_entry_type;
  763. #else
  764. #include <sys/dir.h>
  765. typedef struct direct *directory_entry_type;
  766. #endif
  767.  
  768. /* Declare the routine to get the current working directory.  */
  769.  
  770. #ifndef HAVE_GETCWD
  771. extern char *getwd ();
  772. #define getcwd(b, len)  ((b) ? getwd (b) : getwd (xmalloc (len)))
  773. #else
  774. #ifdef ANSI
  775. extern char *getcwd (char *, int);
  776. #else
  777. extern char *getcwd ();
  778. #endif /* not ANSI */
  779. #endif /* not HAVE_GETWD */
  780.  
  781. #if defined(SYSV) || defined(VMS) || defined(MSDOS) || defined(OS2)
  782. #define MAXPATHLEN (256)
  783. #else   /* ~SYSV */
  784. #include <sys/param.h>          /* for MAXPATHLEN */
  785. #endif
  786.  
  787. extern void exit() ;
  788. extern int chdir() ;
  789.  
  790. /* Memory operations: variants of malloc(3) and realloc(3) that just
  791.    give up the ghost when they fail.  */
  792.  
  793. extern char *realloc ();
  794.  
  795. char *
  796. xmalloc (size)
  797.   unsigned size;
  798. {
  799.   char *mem = malloc (size);
  800.   
  801.   if (mem == NULL)
  802.     {
  803.       fprintf (stderr, "! Cannot allocate %u bytes.\n", size);
  804.       exit (10);
  805.     }
  806.   
  807.   return mem;
  808. }
  809.  
  810.  
  811. char *
  812. xrealloc (ptr, size)
  813.   char *ptr;
  814.   unsigned size;
  815. {
  816.   char *mem = realloc (ptr, size);
  817.   
  818.   if (mem == NULL)
  819.     {
  820.       fprintf (stderr, "! Cannot reallocate %u bytes at %x.\n", size, ptr);
  821.       exit (10);
  822.     }
  823.     
  824.   return mem;
  825. }
  826.  
  827.  
  828. /* Return, in NAME, the next component of PATH, i.e., the characters up
  829.    to the next PATHSEP.  */
  830.    
  831. static void
  832. next_component (name, path)
  833.   char name[];
  834.   char **path;
  835. {
  836.   unsigned count = 0;
  837.   
  838.   while (**path != 0 && **path != PATHSEP)
  839.     {
  840.       name[count++] = **path;
  841.       (*path)++; /* Move further along, even between calls.  */
  842.     }
  843.   
  844.   name[count] = 0;
  845.   if (**path == PATHSEP)
  846.     (*path)++; /* Move past the delimiter.  */
  847. }
  848.  
  849.  
  850. #ifndef _POSIX_SOURCE
  851. #define S_ISDIR(m) ((m & S_IFMT) == S_IFDIR)
  852. #endif
  853.  
  854. /* Return true if FN is a directory or a symlink to a directory,
  855.    false if not. */
  856.  
  857. int
  858. is_dir (fn)
  859.   char *fn;
  860. {
  861.   struct stat stats;
  862.  
  863.   return stat (fn, &stats) == 0 && S_ISDIR (stats.st_mode);
  864. }
  865.  
  866.  
  867. static char *
  868. concat3 (s1, s2, s3)
  869.   char *s1, *s2, *s3;
  870. {
  871.   char *r = xmalloc (strlen (s1) + strlen (s2) + strlen (s3) + 1);
  872.   strcpy (r, s1);
  873.   strcat (r, s2);
  874.   strcat (r, s3);
  875.   return r;
  876. }
  877.  
  878.  
  879. /* DIR_LIST is the default list of directories (colon-separated) to
  880.    search.  We want to add all the subdirectories directly below each of
  881.    the directories in the path.
  882.      
  883.    We return the list of directories found.  */
  884.  
  885. char *
  886. do_subdir_path (dir_list)
  887.   char *dir_list;
  888. {
  889.   char *cwd;
  890.   unsigned len;
  891.   char *result = xmalloc ((unsigned)1);
  892.   char *temp = dir_list;
  893.   char dirsep[2] ;
  894.  
  895.   dirsep[0] = DIRSEP ;
  896.   dirsep[1] = '\0' ;
  897.  
  898.   /* Make a copy in writable memory.  */
  899.   dir_list = xmalloc (strlen (temp) + 1);
  900.   strcpy (dir_list, temp);
  901.   
  902.   *result = 0;
  903.  
  904.   /* Unfortunately, we can't look in the environment for the current
  905.      directory, because if we are running under a program (let's say
  906.      Emacs), the PWD variable might have been set by Emacs' parent
  907.      to the current directory at the time Emacs was invoked.  This
  908.      is not necessarily the same directory the user expects to be
  909.      in.  So, we must always call getcwd(3) or getwd(3), even though
  910.      they are slow and prone to hang in networked installations.  */
  911.   cwd = getcwd (NULL, MAXPATHLEN + 2);
  912.   if (cwd == NULL)
  913.     {
  914.       perror ("getcwd");
  915.       exit (errno);
  916.     }
  917.  
  918.   do
  919.     {
  920.       DIR *dir;
  921.       directory_entry_type e;
  922.       char dirname[MAXPATHLEN];
  923.  
  924.       next_component (dirname, &dir_list);
  925.  
  926.       /* All the `::'s should be gone by now, but we may as well make
  927.          sure `chdir' doesn't crash.  */
  928.       if (*dirname == 0) continue;
  929.  
  930.       /* By changing directories, we save a bunch of string
  931.          concatenations (and make the pathnames the kernel looks up
  932.          shorter).  */
  933.       if (chdir (dirname) != 0) continue;
  934.  
  935.       dir = opendir (".");
  936.       if (dir == NULL) continue;
  937.  
  938.       while ((e = readdir (dir)) != NULL)
  939.         {
  940.           if (is_dir (e->d_name) && strcmp (e->d_name, ".") != 0
  941.               && strcmp (e->d_name, "..") != 0)
  942.             {
  943.               char *found = concat3 (dirname, dirsep, e->d_name);
  944.  
  945.               result = xrealloc (result, strlen (result) + strlen (found) + 2);
  946.  
  947.               len = strlen (result);
  948.               if (len > 0)
  949.                 {
  950.                   result[len] = PATHSEP;
  951.                   result[len + 1] = 0;
  952.                 }
  953.               strcat (result, found);
  954.               free (found);
  955.             }
  956.         }
  957.       closedir (dir);
  958.  
  959.       /* Change back to the current directory, in case the path
  960.          contains relative directory names.  */
  961.       if (chdir (cwd) != 0)
  962.         {
  963.           perror (cwd);
  964.           exit (errno);
  965.         }
  966.     }
  967.   while (*dir_list != 0);
  968.   
  969.   return result;
  970. }
  971. #endif /* SEARCH_SUBDIRECTORIES */
  972.